package com.tuanzi.loan.web.bean.system.workflow;

import static com.tuanzi.loan.business.constants.DictionaryCode.WORKFLOW_FORM_ATTRIBUTE_TYPE;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFBean;
import com.tuanzi.loan.workflow.entity.FormAttribute;
import com.tuanzi.loan.workflow.entity.FormBinding;
import com.tuanzi.loan.workflow.service.FormAttributeService;
import com.tuanzi.loan.workflow.service.FormBindingService;
import com.tuanzi.loan.workflow.service.WorkflowManagerService;
import com.tuanzi.loan.workflow.vo.ActivityResponse;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/22
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class WorkflowFormBean extends JSFBean {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private WorkflowManagerService workflowManagerService;
	@Autowired
	private FormBindingService formBindingService;
	@Autowired
	private FormAttributeService formAttributeService;
	private List<FormBinding> formBindings;
	private List<ActivityResponse> activityResponses;
	private Map<String /* activityId + formBindingId */, FormAttribute> formAttributeMap;
	private String procDefId;

	@PostConstruct
	private void init() {
		formAttributeMap = Maps.newHashMap();
	}

	public void onLoadFormData() {
		formBindings = formBindingService.findAllByProcDefId(procDefId);
		activityResponses = workflowManagerService.findAllUserTaskActivity(procDefId);
		for (FormAttribute formAttribute : formAttributeService.findAllByProcDefId(procDefId)) {
			formAttributeMap.put(formAttribute.getActivityId() + formAttribute.getFormBindId(), formAttribute);
		}
	}

	public FormAttribute getFormAttributeBy(String activityId, Long formBindingId) {
		return formAttributeMap.get(activityId + formBindingId);
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(WORKFLOW_FORM_ATTRIBUTE_TYPE);
	}
}
